<?php
class Admin_UserController extends Zend_Controller_Action {
	public function preDispatch() {
	}
	public function init() {
		$layoutPath = APPLICATION_PATH . '/templates/admin';
		$options = array (
				'layout' => 'index',
				'layoutPath' => $layoutPath 
		);
		Zend_Layout::startMvc ( $options );
		
		if ($this->_request->getActionName () != 'login' || $this->_request->getActionName () != 'logout' || $this->_request->getActionName () != 'profile') {
			$requests = $this->getRequest ()->getParams ();
			$User = new Zend_Session_Namespace ( 'Login' );
			if (! $User->User) {
				$this->view->User=$User->User;
				$this->_forward ( 'login', 'user', 'admin' );
			} else {
				$s = Lib_Security::checkPermission ( $User->User, $requests );
				if (! $s) {
					$this->_redirect ( '.' );
				}
			}
		}
	}
	public function indexAction() {
		$Condition = array ();
		$this->view->Users = Users::getAllWithoutPager ( $Condition );
	}
	public function createAction() {
		$requests = $this->getRequest ()->getParams ();
		if ($this->getRequest ()->isPost ()) {
			try {
				$User = new Users ();
				$User->merge ( $requests );
				$key = Lib_General::getKey ();
				$password = Lib_General::encryptPassword ( $requests ['password'], $key );
				$User->password = $password;
				$User->active_code = $key;
				$User->register_date = date ( 'Y-m-d H:i:s', time () );
				$User->register_ip = $_SERVER ['REMOTE_ADDR'];
				if ($User->trySave ()) {
					if ($_FILES) {
						$uploadDir = PUBLIC_PATH . "/uploads/avatars/";
						$image = self::_uploadFiles ( $uploadDir, $User->id );
						$User->avatar = $image;
						$User->save ();
						$this->view->Message = 'Create user successfull';
					}
				}
			} catch ( Exception $e ) {
				print_r ( $e->getMessage () );
				die ();
			}
		} else {
			unset ( $requests ['id'] );
		}
	}
	private static function _uploadFiles($uploadDir = '', $FileName = '') {
		if (! is_dir ( $uploadDir )) {
			@mkdir ( $uploadDir );
			chmod ( $uploadDir, 0777 );
		}
		// upload thumb image
		$image = new Zend_Form_Element_File ( 'image' );
		$image->setDestination ( $uploadDir );
		$image->addValidator ( 'Count', false, array (
				'min' => 1,
				'max' => 1 
		) );
		$image->addValidator ( 'Extension', false, 'jpg,png,gif' );
		$image->addFilter ( 'Rename', array (
				'target' => $uploadDir . $FileName . '.jpg',
				'overwrite' => true 
		) );
		$image->receive ();
		return $image->getFileName ( null, false );
	}
	private static function _deleteFile($uploadDir = '') {
		if (is_dir ( $uploadDir )) {
			unlink ( $uploadDir );
		}
	}
	public function editAction() {
		$requests = $this->getRequest ()->getParams ();
		
		$User = Users::getById ( $requests ['id'] );
		
		if ($User) {
			$this->view->User = $User;
			if ($this->getRequest ()->isPost ()) {
				try {
					$password = '';
					if ($requests ['password'] == '' || ! isset ( $requests ['password'] )) {
						unset ( $requests ['password'] );
					} else {
						$key = Lib_General::getKey ();
						$password = Lib_General::encryptPassword ( $requests ['password'], $key );
					}
					$User->merge ( $requests );
					
					if ($password != '') {
						$User->password = $password;
						$User->active_code = $key;
					}
					
					$User->register_date = date ( 'Y-m-d H:i:s', time () );
					$User->register_ip = $_SERVER ['REMOTE_ADDR'];
					if ($User->trySave ()) {
						unset ( $requests ['id'] );
						if ($_FILES) {
							$uploadDir = PUBLIC_PATH . "/uploads/users/";
							self::_deleteFile ( $User->getThumbImage () );
							$image = self::_uploadFiles ( $uploadDir, $User->id );
							$User->avatar = $image;
							$User->save ();
							$this->view->Message = 'Update user successfull';
						}
					}
				} catch ( Exception $e ) {
					print_r ( $e->getMessage () );
					die ();
				}
			}
		} else {
			$this->_redirect ( '/admin/user' );
		}
	}
	public function deleteAction() {
		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ( true );
		$json = array (
				'STATUS' => 0 
		);
		try {
			$requests = $this->getRequest ()->getParams ();
			$User = Users::getById ( $requests ['id'] );
			$User->delete ();
			$json ['STATUS'] = 1;
		} catch ( Exception $e ) {
			$json ['STATUS'] = 0;
		}
		header ( 'Content-type: application/json' );
		echo json_encode ( $json );
	}
	public function deletemultiAction() {
		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ( true );
		
		try {
			$requests = $this->getRequest ()->getParams ();
			
			$arrID = $requests ['user'];
			foreach ( $arrID as $key => $value ) {
				
				$User = Users::getById ( $value );
				
				if ($User) {
					$User->delete ();
				}
			}
			$this->_redirect ( '/admin/user/index' );
		} catch ( Exception $e ) {
			print_r ( $e->getMessage () );
			die ();
		}
	}
	public function loginAction() {
		$this->_helper->layout ()->disableLayout ();
		$Session = new Zend_Session_Namespace ( 'Login' );
		
		if ($Session->User) {
			$this->_redirect ( 'admin/user' );
		} else {
			if ($this->getRequest ()->isPost ()) {
				$requests = $this->getRequest ()->getParams ();
				$User = Users::authenticate ( $requests ['username'], $requests ['password'] );
				if ($User) {
					$Auth = new Zend_Session_Namespace ( 'Login' );
					$Auth->User = $User;
					$this->_redirect ( 'admin/user' );
				} else {
					$this->view->Message = "Password isn't valid";
				}
			}
		}
	}
	public function logoutAction() {
		$this->_helper->layout ()->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ();
		$Session = new Zend_Session_Namespace ( 'Login' );
		if ($Session->User) {
			$Session->__unset ( 'User' );
			$this->_redirect ( 'admin/user/login' );
		}
	}
	public function profileAction() {
		$Session = new Zend_Session_Namespace ( 'Login' );
		if ($Session->User) {
			$this->view->User = $Session->User;
			if ($this->getRequest ()->isPost ()) {
				$requests = $this->getRequest ()->getParams ();
				
				$User = $Session->User;
				
				try {
					$password = '';
					if ($requests ['password'] == '' || ! isset ( $requests ['password'] )) {
						unset ( $requests ['password'] );
					} else {
						$key = Lib_General::getKey ();
						$password = Lib_General::encryptPassword ( $requests ['password'], $key );
					}
					$User->merge ( $requests );
					
					if ($password != '') {
						$User->password = $password;
						$User->active_code = $key;
					}
					
					$User->register_date = date ( 'Y-m-d H:i:s', time () );
					$User->register_ip = $_SERVER ['REMOTE_ADDR'];
					if ($User->trySave ()) {
						unset ( $requests ['id'] );
						if ($_FILES) {
							$uploadDir = PUBLIC_PATH . "/uploads/users/";
							self::_deleteFile ( $User->getThumbImage () );
							$image = self::_uploadFiles ( $uploadDir, $User->id );
							$User->avatar = $image;
							$User->save ();
							$this->view->Message = 'Update user successfull';
						}
					}
				} catch ( Exception $e ) {
					print_r ( $e->getMessage () );
					die ();
				}
			}
		}
	}
}







